package com.lzh.others.basejava.IO_18;

import java.io.File;
import java.net.URL;

/**
 * 使用File相关类
 * 创建目录
 * 读文件信息
 * -d,删除path下全部
 * -r,复制path1 到 path2
 */
public class MakeDirectories {
    private static void usage() {
        System.out.println(
                "Usage: MakeDirectories path1 ...\n" +
                        "Creates each path\n" +
                        "Usage: MakeDirectories -d path1 ...\n" +
                        "Deletes each path\n" +
                        "Usage: MakeDirectories -r path1 path2\n" +
                        "Renames from path1 to path2"
        );
        System.exit(1);
    }

    private static void fileData(File f) {
        System.out.println(
                "Absolute path: " + f.getAbsolutePath() +
                        "\nCan read: " + f.canRead() +
                        "\ngetName: " + f.getName() +
                        "\ngetParent: " + f.getParent() +
                        "\ngetPath: " + f.getPath() +
                        "\nlength: " + f.length() +
                        "\nExists: " + f.exists() +
                        "\nIs directory: " + f.isDirectory() +
                        "\nIs file: " + f.isFile() +
                        "\nIs hidden: " + f.isHidden() +
                        "\nLast modified: " + f.lastModified());
        if (f.isFile()) {
            System.out.println("it's a file");
        } else if (f.isDirectory()) {
            System.out.println("it's a directory");
        }
    }

    public static void main(String[] args) {
        args = new String[]{"-r", "D:\\test\\test1", "E:\\test\\test1"};
        Class<?> clazz = Directory.class;
        URL resource = clazz.getResource(clazz.getSimpleName() + ".class");
        File path = new File(resource.getPath()).getParentFile();

        if ("-r".equals(args[0])) {
            File old = new File(args[1]);
            File rename = new File(args[2]);
            old.renameTo(rename);
            fileData(old);
            fileData(rename);
            String s = new String();
            return;
        }

        int count = 0;
        boolean del = false;
        if ("-d".equals(args[0])) {
            count++;
            del = true;
        }
        count--;
        while (++count < args.length) {
            File f = new File(args[count]);
            if (f.exists()) {
                System.out.println(f + " exists");
                if (del) {
                    System.out.println("deleting..." + f);
                    deleteDirectoryRecursively(f);
                }
            } else {//Doesn't exist
                if (!del) {
                    f.mkdirs();
                    System.out.println("created " + f);
                }
            }
            fileData(f);
        }
    }

    /**
     * 递归删除目录及其中的所有文件和子目录。
     *
     * @param file 要删除的目录或文件。
     * @return 如果成功删除返回true，否则返回false。
     */
    public static boolean deleteDirectoryRecursively(File file) {
        if (file == null || !file.exists()) {
            return false;
        }

        // 如果是文件，则直接删除
        if (file.isFile()) {
            return file.delete();
        }

        // 如果是目录，则递归删除其内容
        File[] files = file.listFiles();
        if (files != null) {
            for (File child : files) {
                if (!deleteDirectoryRecursively(child)) {
                    return false;
                }
            }
        }

        // 删除空目录
        return file.delete();
    }
}

